//
//  BaseApi.m
//  RtMart
//
//  Created by 福利购Mac on 16/12/19.
//  Copyright © 2016年 JASON. All rights reserved.
//

#import "BaseApi.h"
#define CustomErrorDomain @"com.xlxsc.XinMart.ErrorDomain"


@implementation BaseApi
#pragma mark - Abstract Method

- (id)create:(id)entity withBlock:(BaseResultBlock)block {
    NSLog(@"You must override %@ in a subclass",NSStringFromSelector(_cmd));
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}

- (id)update:(id)entity withBlock:(BaseResultBlock)block {
    NSLog(@"You must override %@ in a subclass",NSStringFromSelector(_cmd));
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}

- (id)upvote:(id)entity withBlock:(BaseResultBlock)block {
    NSLog(@"You must override %@ in a subclass",NSStringFromSelector(_cmd));
    [self doesNotRecognizeSelector:_cmd];
    return nil;
}

#pragma mark - 请求失败之后提示信息

-(void)showFailMessage:(NSError *)error {
    NSString *strMsg = nil;
    long  errorCode= [error code];
    switch (errorCode)
    {
        case -1009://网络无连接。模拟器一直返回此状态。
            strMsg= @"当前网络不可用，请检查网络设置";
            break;
        case -1001://网络请求超时
            strMsg= @"网络请求超时";
            break;
        default:
            strMsg= @"服务器繁忙，请稍后操作";
            break;
    }
    [[ToastManager shardInstance] showToast:strMsg];
}


- (void)requestDataWithUrl:(NSString *)baseUrl parameters:(NSDictionary *)parameters cacheTypeValue:(NetWorkingCacheType)cache resultBlock:(BaseResultBlock)block {
    [[RTNetWroking shareNetWorking] postRequestWithURL:baseUrl params:parameters cacheType:cache success:^(id responseObject) {
        NSMutableDictionary *data = [(NSDictionary *)responseObject mutableCopy];
        [self successSettings:data resultBlock:block];
    } failure:^(NSError *error) {
        if (block) block(nil, error);
        [self showFailMessage:error];
    }];
}

- (void)requestGetDataWithUrl:(NSString *)baseUrl parameters:(NSDictionary *)parameters cacheTypeValue:(NetWorkingCacheType)cache resultBlock:(BaseResultBlock)block {
    [[RTNetWroking shareNetWorking] getRequestWithURL:baseUrl params:parameters cacheType:cache success:^(id responseObject) {
        NSMutableDictionary *data = [(NSDictionary *)responseObject mutableCopy];
        [self successSettings:data resultBlock:block];
    } failure:^(NSError *error) {
        if (block) block(nil, error);
        [self showFailMessage:error];
    }];
}




- (void)requestJsonDataWithUrl:(NSString *)baseUrl parameters:(NSDictionary *)parameters cacheTypeValue:(NetWorkingCacheType)cache resultBlock:(BaseResultBlock)block {
    [[RTNetWroking shareNetWorking] jsonPost:baseUrl params:parameters cacheType:cache success:^(id responseObject) {
        NSMutableDictionary *data = [(NSDictionary *)responseObject mutableCopy];
        [self successSettings:data resultBlock:block];
    } failure:^(NSError *error) {
        if (block) block(nil, error);
        [self showFailMessage:error];
    }];
}

//上传单张图片
- (void)upLoadSingleFileWithUrl:(NSString *)baseUrl parameters:(NSDictionary *)parameters image:(UIImage *)image resultBlock:(BaseResultBlock)block {
    [[RTNetWroking shareNetWorking] upLoadFileWithURL:baseUrl parameters:parameters image:image successBlock:^(id responseObject) {
        NSMutableDictionary *data = [(NSDictionary *)responseObject mutableCopy];
        [self successSettings:data resultBlock:block];
    } imageUrl:^(NSString *url) {
       
    } faildBlock:^(NSError *error) {
        if (block) block(nil, error);
        [self showFailMessage:error];
    }];
}

//上传多张图片
- (void)upLoadMultiFileWithUrl:(NSString *)baseUrl parameters:(NSDictionary *)parameters imageArray:(NSArray<UIImage *> *)imageArray resultBlock:(BaseResultBlock)block {
    [[RTNetWroking shareNetWorking] upLoadMultiFileWithURL:baseUrl parameters:parameters imageArray:imageArray successBlock:^(id responseObject) {
        NSMutableDictionary *data = [(NSDictionary *)responseObject mutableCopy];
        [self successSettings:data resultBlock:block];
    } imageNames:^(NSString *imageStr) {
      
    } faildBlock:^(NSError *error) {
        if (block) block(nil, error);
        [self showFailMessage:error];
    }];
}

-(void)postStrRequestWithURL:(NSString *)url  parameters:(NSDictionary *)params acheTypeValue:(NetWorkingCacheType)cache resultBlock:(BaseResultBlock)block{
    [[RTNetWroking shareNetWorking]postStrRequestWithURL:url parameters:params successBlock:^(id responseObject) {
         if (block) block(responseObject, nil);
        
    } faildBlock:^(NSError *error) {
        
    }];
}

- (void)needDisplaySVProgressHUD {
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD show];
}

- (void)successSettings:(NSMutableDictionary *)data resultBlock:(BaseResultBlock)block {
    if (!data) return;
//    if ([[NSString stringWithFormat:@"%@",[data objectForKey:@"code"]] isEqualToString:@"300"] && [[NSString stringWithFormat:@"%@",[data objectForKey:@"system_close"]] isEqualToString:@"1"]) {
//        if (!self.systemLockController) {
//            self.systemLockController = [[UIStoryboard LoginStoryboard]instantiateViewControllerWithIdentifier:@"SystemLockController"];
//            [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:self.systemLockController animated:YES completion:nil];
//        }
//        return;
//    }
    BaseModel *model = [BaseModel mj_objectWithKeyValues:data];
    
    if ([[NSString stringWithFormat:@"%@",[data objectForKey:@"status"]] isEqualToString:@"y"]) {
        if (block) block(data, nil);
    }else if (model.code == 5000){
            [[ToastManager shardInstance] showToast:data[@"message"]];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"quitApp" object:nil];
        
    }else {
        
        
//        NSString *message = [data objectForKey:@"message"];
//        if (message.length > 0) {
//            [[ToastManager shardInstance] showToast:message];
//        }
        //自定义一个错误。
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"error"  forKey:NSLocalizedDescriptionKey];
        NSError *error = [NSError errorWithDomain:CustomErrorDomain code:-1000 userInfo:userInfo];
        if (block) block(data, error);
    }
}

@end
